/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package daos;

import businessObjects.Admin;
import daos.exceptions.NonexistentEntityException;
import daos.exceptions.PreexistingEntityException;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;

/**
 *
 * @author williamprado
 */
public class AdminDAO {

    public AdminDAO() {
        //emf = Persistence.createEntityManagerFactory("Adz-ejbPU");
    }
    //private EntityManagerFactory emf = null;

    /*public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }*/

    /*public void create(Admin admin) throws PreexistingEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(admin);
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findAdmin(admin.getLogin()) != null) {
                throw new PreexistingEntityException("Admin " + admin + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }*/

    public void create(Admin admin, EntityManager em) throws PreexistingEntityException, Exception {
        //EntityManager em = null;
        try {
            //em = getEntityManager();
            //em.getTransaction().begin();
            em.persist(admin);
            //em.getTransaction().commit();
        } catch (Exception ex) {
            if (findAdmin(admin.getLogin(), em) != null) {
                throw new PreexistingEntityException("Admin " + admin + " already exists.", ex);
            }
            throw ex;
        }
    }

    /*public void edit(Admin admin) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            admin = em.merge(admin);
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                String id = admin.getLogin();
                if (findAdmin(id) == null) {
                    throw new NonexistentEntityException("The admin with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }*/

    public void edit(Admin admin, EntityManager em) throws NonexistentEntityException, Exception {
        //EntityManager em = null;
        try {
            //em = getEntityManager();
            //em.getTransaction().begin();
            admin = em.merge(admin);
            //em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                String id = admin.getLogin();
                if (findAdmin(id, em) == null) {
                    throw new NonexistentEntityException("The admin with id " + id + " no longer exists.");
                }
            }
            throw ex;
        }
    }

    /*public void destroy(String id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Admin admin;
            try {
                admin = em.getReference(Admin.class, id);
                admin.getLogin();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The admin with id " + id + " no longer exists.", enfe);
            }
            em.remove(admin);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }*/

    public void destroy(String id, EntityManager em) throws NonexistentEntityException, Exception {
        //EntityManager em = null;
        try {
            //em = getEntityManager();
            //em.getTransaction().begin();
            Admin admin;
            try {
                admin = em.getReference(Admin.class, id);
                admin.getLogin();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The admin with id " + id + " no longer exists.", enfe);
            }
            em.remove(admin);
            //em.getTransaction().commit();
        } catch(Exception e) {
            throw e;
        }
    }

    /*public List<Admin> findAdminEntities() {
        return findAdminEntities(true, -1, -1);
    }*/

    public List<Admin> findAdminEntities(EntityManager em) throws Exception {
        return findAdminEntities(true, -1, -1, em);
    }

    /*public List<Admin> findAdminEntities(int maxResults, int firstResult) {
        return findAdminEntities(false, maxResults, firstResult);
    }*/

    public List<Admin> findAdminEntities(int maxResults, int firstResult, EntityManager em) throws Exception {
        return findAdminEntities(false, maxResults, firstResult, em);
    }

    /*private List<Admin> findAdminEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from Admin as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }*/

    private List<Admin> findAdminEntities(boolean all, int maxResults, int firstResult, EntityManager em) throws Exception {
        //EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from Admin as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } catch(Exception e) {
            throw e;
        }
    }

    /*public Admin findAdmin(String id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Admin.class, id);
        } finally {
            em.close();
        }
    }*/

    public Admin findAdmin(String id, EntityManager em) throws Exception {
        //EntityManager em = getEntityManager();
        try {
            return em.find(Admin.class, id);
        } catch(Exception e) {
            throw e;
        }
    }

    /*public int getAdminCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from Admin as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }*/

    public int getAdminCount(EntityManager em) throws Exception {
        //EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from Admin as o");
            return ((Long) q.getSingleResult()).intValue();
        } catch(Exception e) {
            throw e;
        }
    }

}
